package com.z.platform.controller.persistence;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.z.platform.controller.ControllersConfig;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.sql.DataSource;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 21051
 */
public class ConnectionFactory {
    private static Map<String, String> dbConfigMap = new HashMap<>();
    private String driver = null;
    private String url = null;
    private String username = null;
    private String password = null;

    /**
     * 使用静态内部类确保对象对外不可见实现单例模式
     */
    private static class inner {
        private static ConnectionFactory instance = new ConnectionFactory();
    }

    private ConnectionFactory() {
        URL xmlFileUrl = ControllersConfig.class.getClassLoader().getResource("persistence.xml");
        File xmlFile = null;
        if (null != xmlFileUrl) {
            xmlFile = new File(xmlFileUrl.getFile());
        }
        //建立DocumentBuilderFactor，用于获得DocumentBuilder对象：
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        //2.建立DocumentBuidler：
        DocumentBuilder builder = null;
        try {
            builder = factory.newDocumentBuilder();
            //3.建立Document对象，获取树的入口：
            Document doc = builder.parse(xmlFile);
            //4.建立NodeList：
            NodeList nodeList = doc.getElementsByTagName("property");
            //5.进行xml信息获取
            if (null != nodeList && nodeList.getLength() > 0) {
                for (int i = 0; i < nodeList.getLength(); i++) {
                    Node node = nodeList.item(i);
                    NamedNodeMap namedNodeMap = node.getAttributes();
                    String name = namedNodeMap.getNamedItem("name").getNodeValue();
                    String value = namedNodeMap.getNamedItem("value").getNodeValue();
                    dbConfigMap.put(name, value);
                }
            }
            driver = dbConfigMap.get("driverClassName");
            url = dbConfigMap.get("url");
            username = dbConfigMap.get("username");
            password = dbConfigMap.get("password");
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() throws Exception {
        ConnectionFactory instance = inner.instance;
        //使用druid数据源
        DataSource dataSource = DruidDataSourceFactory.createDataSource(dbConfigMap);
        Connection conn = null;
        //Class.forName(instance.driver);
        //3.获取数据库连接对象
        conn = dataSource.getConnection();
        return conn;
    }

}
